package com.ruoyi.kindergarten.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.ruoyi.kindergarten.websocket.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kindergarten.mapper.FoodPurchaseMapper;
import com.ruoyi.kindergarten.domain.FoodPurchase;
import com.ruoyi.kindergarten.service.IFoodPurchaseService;

/**
 * 食材采购记录模块Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-26
 */
@Service
public class FoodPurchaseServiceImpl implements IFoodPurchaseService 
{
    @Autowired
    private FoodPurchaseMapper foodPurchaseMapper;

    @Autowired
    private WebSocketService webSocketService;

    /**
     * 查询食材采购记录模块
     * 
     * @param id 食材采购记录模块主键
     * @return 食材采购记录模块
     */
    @Override
    public FoodPurchase selectFoodPurchaseById(Long id)
    {
        return foodPurchaseMapper.selectFoodPurchaseById(id);
    }

    /**
     * 查询食材采购记录模块列表
     * 
     * @param foodPurchase 食材采购记录模块
     * @return 食材采购记录模块
     */
    @Override
    public List<FoodPurchase> selectFoodPurchaseList(FoodPurchase foodPurchase)
    {
        return foodPurchaseMapper.selectFoodPurchaseList(foodPurchase);
    }

    /**
     * 新增食材采购记录模块
     * 
     * @param foodPurchase 食材采购记录模块
     * @return 结果
     */
    @Override
    public int insertFoodPurchase(FoodPurchase foodPurchase)
    {
        foodPurchase.setUploadTime(new Date());
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String s = objectMapper.writeValueAsString(foodPurchase);
            webSocketService.onMessage(s);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return foodPurchaseMapper.insertFoodPurchase(foodPurchase);
    }

    /**
     * 修改食材采购记录模块
     * 
     * @param foodPurchase 食材采购记录模块
     * @return 结果
     */
    @Override
    public int updateFoodPurchase(FoodPurchase foodPurchase)
    {
        return foodPurchaseMapper.updateFoodPurchase(foodPurchase);
    }

    /**
     * 批量删除食材采购记录模块
     * 
     * @param ids 需要删除的食材采购记录模块主键
     * @return 结果
     */
    @Override
    public int deleteFoodPurchaseByIds(Long[] ids)
    {
        return foodPurchaseMapper.deleteFoodPurchaseByIds(ids);
    }

    /**
     * 删除食材采购记录模块信息
     * 
     * @param id 食材采购记录模块主键
     * @return 结果
     */
    @Override
    public int deleteFoodPurchaseById(Long id)
    {
        return foodPurchaseMapper.deleteFoodPurchaseById(id);
    }
    @Override
    public List<Integer> count() {
        List<Integer> list = new ArrayList<Integer>();
        Integer  heGeSum =  foodPurchaseMapper.count(1);
        Integer  jiJiaGoSum =  foodPurchaseMapper.count(2);
        Integer  buJiGeSum =  foodPurchaseMapper.count(3);
        Integer sun =  heGeSum + jiJiaGoSum + buJiGeSum;
        list.add(sun);
        list.add(heGeSum);
        list.add(jiJiaGoSum);
        list.add(buJiGeSum);
        return list;
    }
}
